<template>
    <div id="app">
        <h1>{{ title }}</h1>
        <div>
            <input @keyup.enter="addList" id="inputVal" placeholder="输入代办事项" type="text" v-model="val">
        </div>
        <div id="div2">
            <select id="sel">
                <option value="1">全部事项</option>
                <option value="2">未完成</option>
                <option value="3">已完成</option>
                <option value="4">已取消</option>
            </select>
            <button id="btns" @click="val=''">下载数据</button>
            <button id="clr" @click="cllist">清空</button>
        </div>
        <!--未完成-->
        <no @upend="upend" @upok="upok" :list="noList"></no>
        <!--已完成-->
        <ok :list="okList"></ok>
        <!--取消-->
        <end @upl="upl" :list="endList"></end>
    </div>
</template>

<script>
import end from './components/endhave.vue'
import no from './components/nohave.vue'
import ok from './components/okhave.vue'

export default {
    data() {
        return {
            title: "记事本",
            val: '',
            endList:[],
            noList:[],
            okList:[]
        }
    },
    components: {
        end,
        no,
        ok
    },
    methods:{
        // 恢复
        upl(id) {
            // 找到对应的对象
            let obj = this.endList.find(item => item.id == id);
            // 删除
            this.endList.splice(this.endList.indexOf(obj), 1);
            // 添加到未完成
            this.noList.push(obj);
            this.saveList()
        },
        // 完成
        upok(id){
            // 找到对应的对象
            let obj = this.noList.find(item => item.id == id);
            // 删除
            this.noList.splice(this.noList.indexOf(obj), 1);
            // 添加到已完成
            this.okList.push(obj);
            this.saveList()
        },
        // 取消
        upend(id){
            // 找到对应的对象
            let obj = this.noList.find(item => item.id == id);
            // 删除
            this.noList.splice(this.noList.indexOf(obj), 1);
            // 添加到已取消
            this.endList.push(obj);
            this.saveList()
        },
        addList(){
            // 把数据插入到 nolist
            this.noList.push({
                id: new Date().getTime(),
                name: this.val
            });
            // 清空输入框
            this.val = ''
            this.saveList()
        },
        // 把当前数据存到 session
        saveList(){
            sessionStorage.setItem('endList',JSON.stringify(this.endList));
            sessionStorage.setItem('noList',JSON.stringify(this.noList));
            sessionStorage.setItem('okList',JSON.stringify(this.okList));
        },
        cllist(){
            //清空存储
            sessionStorage.clear();
            //清空数据
            this.endList = [];
            this.noList = [];
            this.okList = [];
            this.saveList();
        }
    },
    created() {
        //从session中获取list集合
        this.endList = JSON.parse(sessionStorage.getItem('endList')) || [];
        this.noList = JSON.parse(sessionStorage.getItem('noList')) || [];
        this.okList = JSON.parse(sessionStorage.getItem('okList')) || [];

    }
}
</script>

<style>
#app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
}

#inputVal {
    width: 400px;
    height: 30px;
    border: 1px solid;
    border-radius: 8px;
    padding: 5px;
}

#sel {
    width: 100px;
    height: 30px;
    border: 1px solid;
    border-radius: 8px;
    padding: 5px;
}

#btns {
    width: 100px;
    height: 30px;
    border: 1px solid;
    border-radius: 8px;
    padding: 5px;
    margin-left: 10px;
    background-color: #42b983;
    color: white;
}

#clr {
    width: 100px;
    height: 30px;
    border: 1px solid;
    border-radius: 8px;
    padding: 5px;
    margin-left: 10px;
    background-color: #dc3545;
    color: white;
}

#div2 {
    margin-top: 20px;
}
</style>
